// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

// 代表一次请求,包含有请求时的地址参数等信息
class RequestContext {
private:
	char* _url;

	public:
		void setUrl(char* url) {
			_url = url;
		}

		char* getUrl() {
			return _url ;
		}

		void setTransporter(char*(*transporter)(RequestContext)) {
		}

		// 复制传入的RequestContext的所有属性
		RequestContext withRequestContext(RequestContext request) {
			this->setUrl(request.getUrl());
			return *this;
		}
};


// Transporter Http客户端,实现Http通信
char* DefaultCppTransporter(RequestContext request) {
	return (char*)'123';
}


// 方法回调对象
class Deferred :public RequestContext {
	public:
		void then(/*这里应该传入回调函数*/) {
			puts("Deferred.then");
		}

		// 同步执行直接返回结构
		void* result() {
			puts("Deferred.result");
			return (void*)1; // 返回任意类型
		}
};


// 接口代理基类
class AbstractInterfaceDelegate :public RequestContext {
public:
	Deferred invokeMethod(char* interfaceType, char* methodName) {
		return Deferred();
	}
};


// 接口代理,对应服务器端的一个接口,会用模板生成
class OpenTokenApiDelegate :public AbstractInterfaceDelegate {
	public:
		Deferred tryVoid() {
			return AbstractInterfaceDelegate::invokeMethod("OpenTokenApi", "tryVoid");
		}
};

class OpenViewApiDelegate :public AbstractInterfaceDelegate {
	public:
		Deferred tryVoid() {
			return AbstractInterfaceDelegate::invokeMethod("OpenViewApi", "tryVoid");
		}
};


// 入口
class BudoDubboHttpApiCppSdk :public RequestContext {
	public:
		OpenTokenApiDelegate OpenTokenApi() {
			OpenTokenApiDelegate _delegate = OpenTokenApiDelegate();
			 // _delegate.withRequestContext(null);
			 return _delegate;
		}

		OpenViewApiDelegate OpenViewApi() {
			return OpenViewApiDelegate();
		}
};


// 测试
int main()
{
	BudoDubboHttpApiCppSdk sdk = BudoDubboHttpApiCppSdk();
	sdk.setUrl("http://xxx/dubbo-api");
	sdk.setTransporter(DefaultCppTransporter); // 传入HTTP客户端
	sdk.OpenTokenApi().tryVoid().result();
	puts("Hello World!!! 123");
	getchar();
}